//
//  ValidBankListViewController.m
//  HuLaQuan
//
//  Created by hok on 1/20/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ValidBankListViewController.h"

#import "ValidBanksCell.h"

#import "ValidBankListEngine.h"
@interface ValidBankListViewController ()

@property (strong,nonatomic) NSArray *bankList;

@property (strong,nonatomic) ValidBankListEngine *bankListEngine;

@end

static NSString * const ValidBanksCellIdentifier = @"ValidBanksCell";

@implementation ValidBankListViewController

- (ValidBankListEngine *)bankListEngine
{
    if (!_bankListEngine) {
        _bankListEngine = [[ValidBankListEngine alloc] init];
    }
    return _bankListEngine;
}

- (NSArray *)bankList
{
    if (!_bankList) {
        _bankList = [[NSArray alloc] init];
    }
    return _bankList;
    
    
}
- (void)setupUI
{
    [self setupNavBackButton];
    
    self.tableView.backgroundColor = UIColorFromRGBA(kBackgroundColor, 1.0);
    
    
    [self setupFooterForTableView];
    
    [self.tableView registerNib:[ValidBanksCell nib] forCellReuseIdentifier:ValidBanksCellIdentifier];
    
    
    
    self.title = @"支持银行卡列表";
    

}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return kNormalCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bankList.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kUserCenterSectionHeaderHeight;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    ValidBanksCell *cell = [tableView dequeueReusableCellWithIdentifier:ValidBanksCellIdentifier
                                                             forIndexPath:indexPath];
    cell.bankTitle.text = self.bankList[indexPath.row][@"bankName"];
    cell.dayLimit.text = [NSString stringWithFormat:@"单日限额：%@", ServerMoneyToShow(self.bankList[indexPath.row][@"dayQuota"])];
    cell.orderLimit.text = [NSString stringWithFormat:@"单笔限额：%@", ServerMoneyToShow(self.bankList[indexPath.row][@"singleQuota"])];
    
    cell.bankImageName = self.bankList[indexPath.row][@"bankName"];
    return cell;
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

/**
 *  获取支持银行列表
 *
 *  @param notification
 */
- (void)getValidBankListFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        if (messageDic[@"result"]) {
            
            NSLog(@"%@",messageDic[@"result"])
            self.bankList = messageDic[@"result"];
            [self.tableView reloadData];
        }
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
#pragma mark - Life Cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getValidBankListFinish:) name:@"GetValidBankListFinish" object:nil];
    
    
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.bankListEngine getValidBankList];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetValidBankListFinish" object:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
